package com.zzh.log;

import android.text.TextUtils;

import com.zzh.log_annotation.AddLog;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;


/**
 * @author zhangzhihao
 * @email zhangzhihao@gongjiangren.net
 * @date 2020-09-09
 * @desc
 */
@Aspect
public class MethodBehaviorAspect {


    @Pointcut("execution(@com.zzh.log_annotation.AddLog * *(..))")
    public void firstMethodAnnotationBehavior() {
    }

    @Around("firstMethodAnnotationBehavior()")
    public Object wavePointcutAround(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        // 类名
        String className = methodSignature.getDeclaringType().getSimpleName();
        // 方法名
        String methodName = methodSignature.getName();
        // 功能名
        AddLog behaviorTrace = methodSignature.getMethod()
                .getAnnotation(AddLog.class);
        String value = behaviorTrace.value();
        value = TextUtils.isEmpty(value) ? methodName : value;
        long startTime = System.currentTimeMillis();
        Object result = joinPoint.proceed();
        long endTime = System.currentTimeMillis();
        LogU.print(className, value, String.format("耗时：%dms", endTime - startTime));
        return result;
    }
}
